/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.analysys.presto.connector.hbase.adaptive;

import com.analysys.presto.connector.hbase.meta.HBaseConfig;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class HMetadataUtil
{
    private static HMetadataManager hMetadataManager = null;

    private static void initHMetadataManager(HBaseConfig hbaseConfig)
    {
        hMetadataManager = new HMetadataManager(hbaseConfig);
    }

    public static HMetadataManager getHMetadataManager()
    {
        if (hMetadataManager == null) {
            initHMetadataManager(null);
        }
        return hMetadataManager;
    }

    public static HMetadataManager createHMetadataManager(HBaseConfig hbaseConfig)
    {
        if (hMetadataManager == null) {
            initHMetadataManager(hbaseConfig);
        }
        return hMetadataManager;
    }

    public static byte[] serialize(HMetadata data)
            throws Exception
    {
        ObjectOutputStream oos = null;
        ByteArrayOutputStream baos = null;
        try {
            // 序列化
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(data);
            return baos.toByteArray();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                if (baos != null) {
                    baos.close();
                }
                if (oos != null) {
                    oos.close();
                }
            }
            catch (IOException e) {
                throw new Exception(e.getMessage());
            }
        }
        return null;
    }

    public static HMetadata deserialize(byte[] bytes)
            throws Exception
    {
        ByteArrayInputStream bais = null;
        ObjectInputStream ois = null;
        try {
            // 反序列化
            bais = new ByteArrayInputStream(bytes);
            ois = new ObjectInputStream(bais);
            return (HMetadata)ois.readObject();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                if (bais != null) {
                    bais.close();
                }
                if (ois != null) {
                    ois.close();
                }
            }
            catch (IOException e) {
                throw new Exception(e.getMessage());
            }
        }
        return null;
    }
}
